<?php

class Zend_View_Helper_Catalog {

    protected static $menuItems = array(
        'builder'  => 'Стройматериалы',
        'electric' => 'Электрика',
        'repair'   => 'Ремонт инструмента',
        'sanitary' => 'Сантехника',
        'rent'     => 'Аренда инструмента',
    );

    protected static $childAction = array(
        'rentsbycat' => 'rent'
    );

    protected $_action = null;
    static protected  $_selectedGoodsCategory = null;

    protected $view = null;

    function __construct()
    {
        $this->view = new Zend_View();
        $this->view->setScriptPath(APPLICATION_PATH . '/views/scripts/');
        $action = Zend_Controller_Front::getInstance()->getRequest()->getActionName();

        if (isset(self::$childAction[$action]))  {
            $this->_action = self::$childAction[$action];
        } else {
            $this->_action = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
        }
    }


    public function header($categoryType = null, $selectedGoodsCategory = null)
    {
        $this->view->title = self::$menuItems[$this->_action];
        $this->view->logo = $this->_action;
        $this->view->currentCategory = Application_Model_Factory::createCatalogCategory($categoryType ? $categoryType : $this->_action);
        self::$_selectedGoodsCategory = $selectedGoodsCategory;
        return $this->view->render('catalog/header.phtml');
    }

    public function catalog()
    {
        return $this;
    }

    public function getScript()
    {
        switch (true) {
            case $this->isServiceAndRent()  && !$this->isGoodPage():
                $result = '/list';
                break;
            case $this->isGoodPage():
                $result = '/goods';
                break;
            default:
                $result = '/table';
        }

        return $result;
    }

    public function isGoodPage()
    {
        $action = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
        return isset(self::$childAction[$action]);
    }

    public function isServiceAndRent()
    {
        $actionsService = array(
            'rent',
            'repair',
        );

        if ($this->isCatalogPage() AND in_array($this->_action, $actionsService)) {
            return true;
        }

        return false;
    }

    public function isCatalogPage()
    {
        $controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();

        if ($controller == 'catalog') {
            return true;
        }

        return false;
    }

    public function menuItems($noShowActive = true)
    {
        $activeAction = $this->_action;

        if ($noShowActive && isset(self::$menuItems[$activeAction])) {
            unset(self::$menuItems[$activeAction]);
        }

        return self::$menuItems;
    }

    public function getBreadCrumbs()
    {
        $result = '<a class="info-text" href="/">Главная</a> > <a class="info-text"  href="/' . $this->_action . '">' . self::$menuItems[$this->_action] . '</a>';

        if (self::$_selectedGoodsCategory) {
            $result .= ' > <a class="info-text" href="/' . $this->_action  . '/' . self::$_selectedGoodsCategory->getCategory4pu() .'">' . self::$_selectedGoodsCategory->getTitle() . '</a>';
        }

        return $result;
    }

    public function getTplGoods()
    {
        $path = 'goods/';
        $action = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();

        switch ($action) {
            case 'rentsbycat':
                if ($params['category'] == 'almaznoe_burenie') {
                    return $path. 'almaz.phtml';
                }

                return $path . 'rent.phtml';
        }

        return '';
    }
}